<template>
  <div class="list" ref="wrapper">
    <div>
      <div class="current-city">
        <div class="city-title">当前城市</div>
        <div class="city-item">
          <div class="city-name" @click="backHome">{{this.currentCity}}</div>
        </div>
      </div>
      <div class="hot-city">
        <div class="city-title">热门城市</div>
        <div class="city-item"
          v-for="item of hotCities"
          :key="item.id"
          @click="handleCityClick(item.name)">
          <div class="city-name">{{item.name}}</div>
        </div>
      </div>
      <div class="city-list" v-for="(value,key) of cities" :key="key" :ref="key">
        <div class="city-title">{{key}}</div>
        <div class="city-item" v-for="item of value" :key="item.id" @click="handleCityClick(item.name)">
          <div class="city-name">{{item.name}}</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import Bscroll from 'better-scroll'
import { mapState, mapMutations } from 'vuex'
export default {
  name: 'CityList',
  props: {
    hotCities: Array,
    cities: Object,
    letter: String
  },
  computed: {
    ...mapState({
      currentCity: 'city'
    })
  },
  methods: {
    backHome () {
      this.$router.push('/')
    },
    handleCityClick (city) {
      this.changeCity(city)
      this.$router.push('/')
    },
    ...mapMutations(['changeCity'])
  },
  watch: {
    letter () {
      if (this.letter) {
        const element = this.$refs[this.letter][0]
        this.scroll.scrollToElement(element)

      }
    }
  },
  mounted () {
    this.scroll = new Bscroll(this.$refs.wrapper,{click:true})
  }
}

</script>

<style lang="stylus" scoped>
  .list
    overflow: hidden
    position: absolute
    top: 5.5rem
    left: 0
    right: 0
    bottom: 0
  .current-city
    overflow: hidden
    .city-title
      line-height: 2rem
      padding-left: 1rem
      background: #eee
    .city-item
      display: flex
      padding-left: .2rem
      margin: .5rem 0
      .city-name
        width: 6rem
        text-align: center
        background: #ccc
        padding: .2rem
        margin: .1rem
        border-radius: .5rem
  .hot-city
    overflow: hidden
    display: flex
    flex-wrap: wrap
    .city-title
      width: 100%
      line-height: 2rem
      padding-left: 1rem
      background: #eee
    .city-item
      padding-left: .2rem
      .city-name
        width: 5.5rem
        text-align: center
        padding: .1rem
        margin: .1rem
  .city-list
    overflow: hidden
    display: flex
    flex-wrap: wrap
    .city-title
      width: 100%
      line-height: 2rem
      padding-left: 1rem
      background: #eee
    .city-item
      padding-left: .2rem
      .city-name
        width: 5.5rem
        text-align: center
        padding: .1rem
        margin: .1rem

</style>




